/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.caelum.dao;

import br.com.caelum.model.Recurso;
import br.com.caelum.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author estagiario
 */
public class RecursoDao extends GenericDao<Recurso> {

    @Override
    protected Class<Recurso> getClassePersistente() {
        return Recurso.class;
    }
    
    public List buscarRecurso(String descricao, int id) {
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        sessao.beginTransaction();
        Query query = sessao.createQuery("from Recurso as r where r.descricao = :descricao "
                + "and r.entidade = :id_entidade");
        query.setString("descricao", descricao);
        query.setInteger("id_entidade", id);
        List list = query.list();
        sessao.getTransaction().commit();
        return list;
    }
//    public Recurso buscarRecurso(String descricao, int id) {
//        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
//        sessao.beginTransaction();
//       Recurso lista =  (Recurso) sessao.createCriteria(Recurso.class).
//                add(Restrictions.ilike("descricao", descricao, MatchMode.ANYWHERE))
//               .add(Restrictions.eq("entidade", id)).uniqueResult();
//        sessao.getTransaction().commit();
//        return lista;
//    }
}
